//
//  HYMyBookingRoomDetailVC.m
//  Booking
//
//  Created by Maybe_文仔 on 2018/9/11.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYMyBookingRoomDetailVC.h"
#import "HYImageTableViewCell.h"
#import "HYMeetingScheduleVC.h"
#import "HYMeetingInfoCell.h"
#import "HYMeetingRoomInfoModel.h"
#import "HYTextFieldCellModel.h"
#import "HYTextFieldTableViewCell.h"
#import "HYRoomDetailModel.h"

@interface HYMyBookingRoomDetailVC ()

/** dataSource */
@property (nonatomic,strong) HYTableViewDataSource *dataSource;
/** delegate */
@property (nonatomic,strong) HYTableViewDelegate *delegate;
/** dataSets数据源 */
@property (nonatomic,strong) NSMutableArray *dataSetsArray;
@property (nonatomic,strong) NSMutableArray *sectionArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *backBtn;
@property (nonatomic,strong) UIButton *confirmBtn;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UIView *footerView;

@end

@implementation HYMyBookingRoomDetailVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = KAPP_WHITE_COLOR;
    KAdjustsScrollViewInsets_NO(self, self.tableView);
    [self setupSubviews];
    [self registerTableViewCell];
    [self setupData];
}

#pragma mark - setStatusBar
- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)setupSubviews{
    
    self.dataSource.dataSource = self.dataSetsArray;
    self.delegate.dataSource = self.dataSetsArray;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.backBtn];
    [self.footerView addSubview:self.confirmBtn];
    [self.footerView addSubview:self.cancelBtn];
    
    if ([self.myBookingRoomModel.signStatus intValue] == 1) {
        self.tableView.tableFooterView = self.footerView;
    }
    else{
        self.tableView.tableFooterView = nil;
    }
    
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(10 * WIDTH_MULTIPLE);
        make.size.mas_equalTo(CGSizeMake(30 * WIDTH_MULTIPLE, 30 * WIDTH_MULTIPLE));
        make.top.equalTo(self.view).offset(KSTATUSBAR_HEIGHT);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.footerView).offset(-15 * WIDTH_MULTIPLE);
        make.left.equalTo(self.footerView).offset(15 * WIDTH_MULTIPLE);
        make.top.equalTo(self.footerView).offset(30);
        make.height.mas_equalTo(46);
    }];
    
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.confirmBtn);
        make.top.equalTo(self.confirmBtn.mas_bottom).offset(15);
        make.height.mas_equalTo(46);
    }];
}

- (void)setupData{
    [self requestMyBookMeetingDetail];
}

- (void)registerTableViewCell{
    
    [self.tableView registerClass:[HYImageTableViewCell class] forCellReuseIdentifier:@"HYImageTableViewCell"];
    [self.tableView registerClass:[HYMeetingInfoCell class] forCellReuseIdentifier:@"HYMeetingInfoCell"];
    [self.tableView registerClass:[HYTextFieldTableViewCell class] forCellReuseIdentifier:@"HYTextFieldTableViewCell"];
    [self.delegate setHeaderHeightBlock:^CGFloat(UITableView *tableView, NSInteger section) {
        if (section == 1) {
            return 45;
        }
        return 0;
    }];
    
    [self.delegate setHeaderViewBlock:^UIView *(UITableView *tableView, NSInteger section) {
        if (section == 1) {
            UILabel *headView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
            headView.backgroundColor = RGBColor(242, 242, 248);
            headView.font = KFont(15);
            headView.textColor = RGBColor(136, 136, 136);
            headView.text = [NSString stringWithFormat:@"  %@",NSLocalizedString(@"roomInfo", nil)];
            return headView;
        }
        return nil;
    }];

}

- (void)requestMyBookMeetingDetail{
    
    [HYNetRequestHandle getRoomDetailWithMeetingID:self.myBookingRoomModel.meetingroomId complection:^(HYBaseNetworkModel *baseModel) {
        
        HYRoomDetailModel *model = [HYRoomDetailModel modelWithDictionary:baseModel.result];
        
        NSMutableArray *sectionArray = [NSMutableArray array];
        HYImageCellModel *imageCellModel = [HYImageCellModel new];
        imageCellModel.cellIdientifier = @"HYImageTableViewCell";
        imageCellModel.cellHeight = 230 * WIDTH_MULTIPLE;
        imageCellModel.imgUrl = model.imgUrl;
        imageCellModel.isBgImg = YES;
        [sectionArray addObject:imageCellModel];
        
        HYMeetingRoomInfoModel *meetingRoomInfoModel = [HYMeetingRoomInfoModel new];
        meetingRoomInfoModel.cellIdientifier = @"HYMeetingInfoCell";
        meetingRoomInfoModel.cellHeight = 200 * WIDTH_MULTIPLE;
        meetingRoomInfoModel.roomName = model.name;
        meetingRoomInfoModel.floorNo = model.floorName;
        meetingRoomInfoModel.buildingName = model.buildName;
        meetingRoomInfoModel.city = model.cityName;
        meetingRoomInfoModel.capacity = model.capacity;
        meetingRoomInfoModel.tel = model.tel;
        meetingRoomInfoModel.device = model.type;
        [sectionArray addObject:meetingRoomInfoModel];
        
        NSMutableArray *infoSectionArr = [NSMutableArray array];
        NSArray *arr = @[NSLocalizedString(@"status", nil),
                         NSLocalizedString(@"dateTime", nil),
                         NSLocalizedString(@"meetingName", nil),
                         NSLocalizedString(@"organizer", nil),
                         NSLocalizedString(@"invitees", nil),
                         NSLocalizedString(@"remark", nil)];
        NSString *status;
        switch ([self.myBookingRoomModel.signStatus integerValue]) {
            case 1:
                status = NSLocalizedString(@"confirmStatus", nil);
                break;
            case 2:
                status = NSLocalizedString(@"currentStatus", nil);
                break;
            case 3:
                status = NSLocalizedString(@"expiredStatus", nil);
                break;
                
            default:
                status = @"未知";
                break;
        }
        NSString *date = [NSString stringWithFormat:@"%@ ~ %@",self.myBookingRoomModel.startTime,[self.myBookingRoomModel.endTime substringFromIndex:11]];
        
        NSArray *valueArr = @[status,date,self.myBookingRoomModel.markName ?: @"",self.myBookingRoomModel.organizationman ?: @"",self.myBookingRoomModel.attendees ?: @"",self.myBookingRoomModel.remark ?: @""];
        for (NSInteger i = 0; i < arr.count; i++) {
            
            HYTextFieldCellModel *textFieldCellModel = [HYTextFieldCellModel new];
            textFieldCellModel.cellIdientifier = @"HYTextFieldTableViewCell";
            textFieldCellModel.cellHeight = 50;
            textFieldCellModel.title = arr[i];
            textFieldCellModel.value = valueArr[i];
            textFieldCellModel.inputMode = HYTextFieldCellInputModeNotEdit;
            [infoSectionArr addObject:textFieldCellModel];
        }
        [self.dataSetsArray addObject:sectionArray];
        [self.dataSetsArray addObject:infoSectionArr];
        [self.tableView reloadData];
    }];
}

#pragma mark - action
- (void)confirmBtnAction{
    [HYNetRequestHandle signInWithRoomID:self.myBookingRoomModel.id complection:^(BOOL isSuccess) {
        if (isSuccess) {

            [MBProgressHUD showSuccess:NSLocalizedString(@"signSuccess", nil) toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        else{
            [MBProgressHUD showSuccess:NSLocalizedString(@"signError", nil) toView:self.view];
        }
    }];
}

- (void)cancelBtnAction{
    [HYNetRequestHandle cancelMyBookingRoomWithMeetingID:self.myBookingRoomModel.id complection:^(BOOL isSuccess) {
        
        if (isSuccess) {
            
            [MBProgressHUD showSuccess:NSLocalizedString(@"cancelSuccess", nil) toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        else{
            [MBProgressHUD showSuccess:NSLocalizedString(@"cancelError", nil) toView:self.view];
        }
    }];
}


#pragma mark - lazyload
- (HYTableViewDataSource *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [HYTableViewDataSource new];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSetsArray{
    
    if (!_dataSetsArray) {
        _dataSetsArray = [NSMutableArray array];
    }
    return _dataSetsArray;
}

- (NSMutableArray *)sectionArray{
    
    if (!_sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}

- (HYTableViewDelegate *)delegate{
    
    if (!_delegate) {
        _delegate = [HYTableViewDelegate new];
    }
    return _delegate;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = KAPP_WHITE_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self.delegate;
        _tableView.dataSource = self.dataSource;
    }
    return _tableView;
}

- (UIButton *)backBtn{
    
    if (!_backBtn) {
        
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIButton *)confirmBtn{
    
    if (!_confirmBtn) {
        
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundColor:KAPP_THEME_COLOR];
        [_confirmBtn setTitle:NSLocalizedString(@"sign", nil) forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:KAPP_WHITE_COLOR forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = KFitFont(18);
        [_confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.layerCornerRadius = 4;
    }
    return _confirmBtn;
}

-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setBackgroundColor:KAPP_WHITE_COLOR];
        [_cancelBtn setTitle:NSLocalizedString(@"cancelBooking", nil) forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:KAPP_THEME_COLOR forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = KFitFont(18);
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.layerCornerRadius = 4;
        _cancelBtn.layerBorderWidth = 1;
        _cancelBtn.layerBorderColor = KAPP_THEME_COLOR;
    }
    return _cancelBtn;
}

-(UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)];
        _footerView.backgroundColor = RGBColor(242, 242, 248);
    }
    return _footerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
